package com.star.starparent;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

public class Test {

    public static void main(String[] args) throws Exception {
        CountDownLatch countDownLatch = new CountDownLatch(5);
    //    Executor executor = Executors.newFixedThreadPool(5);
        //测试阻塞其他线程
        new Thread(new MyRunnable(countDownLatch)).start();
        //为了测试效果进行线程休眠
        Thread.sleep(1000);
        for(int i = 1;i<=5;i++){
            System.out.println(Thread.currentThread().getName());

            countDownLatch.countDown();

            //为了测试效果进行线程休眠
            Thread.sleep(1000);
            System.out.println("第"+i+"调用countDown方法结束");
        }

		/*
		 * 测试阻塞主线程

		for (int i = 1; i <= 5; i++) {
			new Thread(new MyRunnable1(countDownLatch,i+"")).start();
			Thread.sleep(1000);
		}
		try {
			System.out.println("主线程阻塞");
			countDownLatch.await();
			System.out.println("主线程继续执行");
		} catch (InterruptedException e) {
			e.printStackTrace();
		} */
    }


}

class MyRunnable implements Runnable {
    CountDownLatch countDownLatch;
    public MyRunnable(CountDownLatch countDownLatch) {
        this.countDownLatch = countDownLatch;
    }
    @Override
    public void run() {
        try {
            System.out.println(Thread.currentThread().getName());

            System.out.println("进入线程，即将进入阻塞状态");
            //调用await进行线程阻塞
            countDownLatch.await();
            System.out.println("线程进行执行...");
            System.out.println(Thread.currentThread().getName());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

